﻿using SCG.General;
using System;
using System.Net;

namespace SCG.SolarVengeanceEngine.StarShips
{
    //StarTillery
    public class StarTillery : StarShip
    {
        //constructor
        public StarTillery(SVGame game, Player owner, int engines, int range, StarSystem origin)
            : base(game, owner, engines, range, origin)
        {
        }

        //ship name
        public override string Name
        {
            get
            {
                return "StarTillery";
            }
        }

        //description
        public override string Description
        {
            get
            {
                return "StarTillery will damage the closest enemy StarShip within their Range.  Each Impulse they act (determined by Engines) they will inflict 1 point of damage to a target in range.";
            }
        }

        //ship type
        public override StarShipType ShipType
        {
            get
            {
                return StarShipType.StarTillery;
            }
        }

        //StarTillery damages closest enemy ship
        protected internal override bool MoveProcessing()
        {
            if (ProhibitAction)
                return false;

            //find closest enemy StarShip within range
            StarShip closest = null;
            double maxDist = Double.MaxValue;
            foreach(StarShip ship in Game.StarShips)
            {
                if (!Owner.FriendlyTo(ship.Owner))
                    if (ship.VisibleTo[Owner] && !ship.Destroyed)
                    {
                        //exclude this candidate if its a known ship type that is immune to StarTillery
                        if (ship.DiscoveredBy[Owner])
                            if (ship.ShipType == StarShipType.EMine || ship is Missile)
                                continue;

                        //is it within range?
                        double dist = Distance(ship);
                        if (dist <= Value)
                        {
                            //is it closer than previous considered target?
                            if (dist < maxDist)
                            {
                                //yes, this is the new target
                                maxDist = dist;
                                closest = ship;
                            }
                        }
                    }
            }

            //proceed if we have a target
            if (closest != null)
            {
                //don't damage immune ships
                if (closest.ShipType == StarShipType.EMine || closest is Missile)
                    return false;

                //damage the target
                closest.Damage(1, true);            
            }

            return false;
        }

        //radius color
        public override SCGColor RadiusColor
        {
            get
            {
                return _radiusColor;
            }
        }

        //private members
        private static SCGColor _radiusColor = new SCGColor(128, 255, 0, 0);
    }
}
